from pyramid.config import Configurator
from sqlalchemy import engine_from_config

from scp.modelo.models import initialize_sql

from scp.controlador.views import *

def main(global_config, **settings):
    """ This function returns a WSGI application.
    """
    engine = engine_from_config(settings, 'sqlalchemy.')
    initialize_sql(engine)
    config = Configurator(settings=settings)
    config.add_static_view('static', 'scp:static', cache_max_age=3600)
    config.add_route('view_wiki', '/')
    
    config.add_route('login', '/login')
    config.add_route('logout', '/logout')
    
    config.add_route('view_page', '/{pagename}')
    config.add_route('add_page', '/add_page/{pagename}')
    config.add_route('edit_page', '/{pagename}/edit_page')
    
    config.add_view('scp.controlador.views.view_wiki', route_name='view_wiki')
    
    config.add_view('scp.controlador.login.login', route_name='login', 
                    renderer='scp:templates/login.pt')
    config.add_view('scp.controlador.login.logout', route_name='logout')
    
    
    config.add_view('scp.controlador.views.view_page', route_name='view_page',
                    renderer='scp:templates/view.pt')
    config.add_view('scp.controlador.views.add_page', route_name='add_page',
                    renderer='scp:templates/edit.pt')
    config.add_view('scp.controlador.views.edit_page', route_name='edit_page',
                    renderer='scp:templates/edit.pt')
                    
                    
    return config.make_wsgi_app()
